package com.c35.ajax.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckServlet extends HttpServlet {

    private static String[] usernameList = new String[] { "yushan", "shuany", "shuanys" };

    private static enum DataType {
        xml, html, json
    };

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String username = (String) request.getParameter("username");
        String datatype = (String) request.getParameter("datatype");

        switch (DataType.valueOf(datatype.toLowerCase())) {
        case xml:
            doByXml(response, username);
            break;
        case html:
            doByHtml(response, username);
            break;
        case json:
            doByJson(response, username);
            break;
        }
    }

    private void doByHtml(HttpServletResponse response, String username) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        String result;
        if (username.equals("") || username == null) {
            result = "false$$Username is required!";
        } else if (this.isContain(username)) {
            result = "false$$already exists,Please choose other username!";
        } else {
            result = "true$$is available";
        }
        response.getWriter().write(result);
    }

    private void doByXml(HttpServletResponse response, String username) throws IOException {
        response.setContentType("text/xml;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        StringBuilder xml = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        if (username.equals("") || username == null) {
            xml.append("<message><result>false</result><info>Username is required!</info></message>");
        } else if (this.isContain(username)) {
            xml.append("<message><result>false</result><info>already exists,Please choose other username!</info></message>");
        } else {
            xml.append("<message><result>true</result><info>is available</info></message>");
        }
        response.getWriter().write(xml.toString());
    }

    private void doByJson(HttpServletResponse response, String username) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        String result;
        if (username.equals("") || username == null) {
            result = "{\"result\":\"false\", \"info\":\"Username is required!\"}";
        } else if (this.isContain(username)) {
            result = "{\"result\":false\", \"info\":\"already exists,Please choose other username!\"}";
        } else {
            result = "{\"result\":\"true\", \"info\":\"is available\"}";
        }
        response.getWriter().write(result);
    }

    public boolean isContain(String param) {
        for (String username : usernameList) {
            if (username.toLowerCase().equals(param)) {
                return true;
            }
        }
        return false;
    }

}
